{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "db31f5b5",
   "metadata": {},
   "source": [
    "### 具名元组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d71795c8",
   "metadata": {},
   "source": [
    "collections.namedtuple是一个工厂函数，它可以用来构建一个带字段名的元组和一个有名字的类——该类对调试程序有很大的帮助。\n",
    "\n",
    "- 用nametuple构建的类的实例所消耗的内存与元组一样，字段名被存在对应的类里面。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8cef8e2",
   "metadata": {},
   "source": [
    "下面是用具名元组记录一个城市信息的示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "facfcc73",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a(name='Tokyo', country='JP', population=36.933, coordinates=(35.689722, 139.691667))\n",
      "<class 'type'>\n",
      "<class '__main__.a'>\n",
      "36.933\n",
      "(35.689722, 139.691667)\n",
      "JP\n"
     ]
    }
   ],
   "source": [
    "from collections import namedtuple\n",
    "\n",
    "City = namedtuple('a', 'name country population coordinates')\n",
    "tokyo = City('Tokyo', 'JP', 36.933, (35.689722, 139.691667))\n",
    "print(tokyo)\n",
    "print(type(City))\n",
    "print(type(tokyo))\n",
    "\n",
    "print(tokyo.population)\n",
    "print(tokyo.coordinates)\n",
    "print(tokyo[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "220fdf29",
   "metadata": {},
   "source": [
    "上面创建一个具名元组需要两个参数，一个是类名，另一个是类中"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "681bf0a7",
   "metadata": {},
   "source": [
    "Temp，字典测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9f51b8e6",
   "metadata": {},
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: '-f'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-3-fab9896b66ea>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[0mindex\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[1;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msys\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0margv\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mencoding\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'utf-8'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mfp\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      8\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0mline_no\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mline\u001b[0m \u001b[1;32min\u001b[0m \u001b[0menumerate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfp\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m         \u001b[1;32mfor\u001b[0m \u001b[0mmatch\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mWORD_RE\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfinditer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mline\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: '-f'"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "import re\n",
    "\n",
    "WORD_RE = re.compile(r'\\w')\n",
    "\n",
    "index = {}\n",
    "with open(sys.argv[1], encoding='utf-8') as fp:\n",
    "    for line_no, line in enumerate(fp, 1):\n",
    "        for match in WORD_RE.finditer(line):\n",
    "            word = match.group()\n",
    "            column_no = match.start()+1\n",
    "            location = (line_no, column_no)\n",
    "            occurrences = index.get(word, [])\n",
    "            occurrences.append(location)\n",
    "            index[word] = occurrences\n",
    "            \n",
    "for word in sorted(index, key=str.upper):\n",
    "    print(word, index[word])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
